package com.link.base.wechat.wxcard.controller;

import com.alibaba.fastjson.JSON;
import com.link.base.wechat.wxcard.model.CardActive;
import com.link.base.wechat.wxcard.model.WxCardInfo;
import com.link.base.wechat.wxcard.model.WxUpdateMemberInfo;
import com.link.base.wechat.wxcard.service.WxCardInfoService;
import com.link.core.cllog.LCLogger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author huanglongjian
 * @Date 2019-11-04 10:17
 */


@Controller
@RequestMapping("/link/wxcardinfo")
public class WxCardInfoController {

    @Resource
    private WxCardInfoService wxCardInfoService;


    /**
     * 更新微信卡信息
     * 
     * @author huanglongjian
     * @date 2019-11-04 10:20
     */
    @RequestMapping(value = {"/upsertWxCard"}, method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> upsertWxCard(@RequestParam Map<String, Object> wxCard, HttpSession session,
                                            HttpServletRequest request) throws Exception {

        Map<String, Object> result = new HashMap<>(16);
        try {
            String wxAppId = (String) wxCard.get("wxAppId");
            WxCardInfo wxCardInfo = JSON.parseObject((String) wxCard.get("wxCardInfo"), WxCardInfo.class);
            String rowStatus = (String) wxCard.get("rowStatus");
            Map<String, Object> ret = wxCardInfoService.upsertWxCard(wxAppId, wxCardInfo, rowStatus);
            result.put("success", true);
            result.put("result", ret);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 激活微信卡
     *
     * @author huanglongjian
     * @date 2019-11-04 10:20
     */
    @RequestMapping(value = {"/activeCard"}, method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> activeCard(@RequestParam Map<String, Object> wxCard, HttpSession session,
                                          HttpServletRequest request) throws Exception {

        Map<String, Object> result = new HashMap<>(16);
        try {
            String wxAppId = (String) wxCard.get("wxAppId");
            CardActive cardActive = JSON.parseObject((String) wxCard.get("cardActive"), CardActive.class);
            int ret = wxCardInfoService.activeCard(wxAppId, cardActive);
            result.put("success", true);
            result.put("result", ret);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 更新会员信息
     *
     * @author huanglongjian
     * @date 2019-11-04 10:20
     */
    @RequestMapping(value = {"/updateMember"}, method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateMember(@RequestParam Map<String, Object> wxMember, HttpSession session,
                                            HttpServletRequest request) throws Exception {

        Map<String, Object> result = new HashMap<>(16);
        try {
            String wxAppId = (String) wxMember.get("wxAppId");
            WxUpdateMemberInfo wxUpdateMemberInfo = JSON.parseObject((String) wxMember.get("wxUpdateMemberInfo"), WxUpdateMemberInfo.class);
            int ret = wxCardInfoService.updateMember(wxAppId, wxUpdateMemberInfo);
            result.put("success", true);
            result.put("result", ret);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
